<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增维修')"/>
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">

                <div class="layui-form-item">
                    <label class="layui-form-label">欲领用设备</label>
                    <div class="layui-input-block">
                        <ul id="deviceId" name="deviceId" class="dtree" data-id="0"></ul>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">申请时间</label>
                    <div class="layui-input-block">
                        <input class="layui-input" id="createTime" autocomplete="off" name="createTime"/>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">归还时间</label>
                    <div class="layui-input-block">
                        <input class="layui-input" id="returnTime" autocomplete="off" name="returnTime"/>
                    </div>
                </div>

                <div class="layui-form-item" style="padding-top: 50px">
                    <label class="layui-form-label">申请理由</label>
                    <div class="layui-input-block">
                        <textarea class="layui-textarea" rows="5" name="remark" lay-verify="required" maxlength="500"
                                  autocomplete="off"></textarea>
                    </div>
                </div>

            </div>
        </div>
    </div>

    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="user-update">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">

    layui.use(['form', 'jquery', 'dtree', 'laydate'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let dtree = layui.dtree;
        let laydate = layui.laydate;

        var day = new Date();

        function formatDate(date) {
            var myyear = date.getFullYear();
            var mymonth = date.getMonth() + 1;
            var myweekday = date.getDate();

            if (mymonth < 10) {
                mymonth = "0" + mymonth;
            }
            if (myweekday < 10) {
                myweekday = "0" + myweekday;
            }
            return (myyear + "-" + mymonth + "-" + myweekday);
        }

        //执行一个日期实例
        laydate.render({
            elem: '#createTime' //指定元素
            , min: day.toDateString()
            , done: function (value) {
                //执行一个日期实例
                laydate.render({
                    elem: '#returnTime' //指定元素
                    , min: value
                });
            }
        });


        //初始化用户树
        dtree.renderSelect({
            elem: "#deviceId",
            url: "/device/into/tree",
            method: 'get',
            selectInputName: {nodeId: "id", context: "deviceName"},
            skin: "layui",
            dataFormat: "list",
            response: {treeId: "id", parentId: "parentId", title: "deviceName"},
            selectInitVal: "1",
            ficon: "9",
            width: "400"
        });

        form.on('submit(user-update)', function (data) {
            $.ajax({
                url: '/device/into/save',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.success) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("user-table");
                        });
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });
    })
</script>
</body>
</html>